<?php
    class CountryController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
         
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'country','default');
        }

        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $countrysCount = array();

            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
                
                
            } 

            $countryModel = new Model_Country();
            $countrysData = $countryModel->fetchLISTCountry($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $countrysCount = $countryModel->fetchLISTCountry($where);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal(count($countrysCount));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'country', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->countrysData = $countrysData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $countryModel->getStatusArray();
        }
        
        public function deleteAction($ids = Array()) {
            //Load country Model 
            $countryModel = new Model_Country();
            if ($this->_getParam('country_ref_id') && $this->_getParam('country_ref_id') != '') {
                $country_ref_id = $this->_getParam('country_ref_id');
                $countryModel->deleteCountry($country_ref_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'country', 'default');
            } else {
                $countryModel->deleteCountry($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'country', 'default');
            }
        }
     public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $countryData = Array();
            $this->view->mode = 'Add';

            //Load country Model
            $countryModel = new Model_Country();

            //Load country Form
           //$countryForm = new Form_CountryForm();
             $languageModel = new Model_Language();
             /*$languageArray = $languageModel->fetchlanguageArray();
             $countryForm->country_lang_id->addMultiOptions($languageArray);
        
            //Load the status array for the select box in form 
            $statusArray = $countryModel->getStatusArray();
            $countryForm->country_status->addMultiOptions($statusArray);
            */
             
             $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['country_identity_code'] = "";
                $languages[$_lan['lang_id']]['country_name'] = "";
                $languages[$_lan['lang_id']]['country_title'] = "";
                $languages[$_lan['lang_id']]['country_2_code'] = "";
                $languages[$_lan['lang_id']]['country_3_code'] = "";
                $languages[$_lan['lang_id']]['country_ref_id'] = "";
                $languages[$_lan['lang_id']]['country_id'] = "";
            }
            //Check for the country Parameter
            $countryData['country_ref_id'] = $this->_request->getParam('country_ref_id');
            $country_ref_id = (!empty($countryData['country_ref_id'])) ? $countryData['country_ref_id'] : null;
            
            if (!empty($country_ref_id)) {
                //Set the Mode to Edit
                $this->view->mode = 'Edit';
                //Fetch data to be loaded in form 
                $data = $countryModel->fetchEntryByREFId($country_ref_id);
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['country_lang_id'], $languages)) {
                        $languages[$_desc['country_lang_id']]['country_identity_code'] = $_desc['country_identity_code'];
                        $languages[$_desc['country_lang_id']]['country_name'] = $_desc['country_name'];
                        $languages[$_desc['country_lang_id']]['country_title'] = $_desc['country_title'];
                        $languages[$_desc['country_lang_id']]['country_2_code'] = $_desc['country_2_code'];
                        $languages[$_desc['country_lang_id']]['country_3_code'] = $_desc['country_3_code'];
                        $languages[$_desc['country_lang_id']]['country_id'] = $_desc['country_id'];
                        $languages[$_desc['country_lang_id']]['country_ref_id'] = $country_ref_id;
                        
                        
                    }
                }
                
            }
            //Populate form 
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
               
               /* if($postedData['country_identity_code']== ''){
                    $errorMessageArray[] = 'Please Enter identity code';
                }
                if($postedData['country_name']== ''){
                    $errorMessageArray[] = 'Please enter name';
                }*/
                if($postedData['country_2_code']== '' || $postedData['country_3_code']== ''){
                    $errorMessageArray[] = 'Please enter code';
                }
               /* $languages[1]['country_identity_code'] = $postedData['country_identity_code'];
                $languages[1]['country_name'] = $postedData['country_name'];
                */
                $languages[1]['country_2_code'] = $postedData['country_2_code'];
                $languages[1]['country_3_code'] = $postedData['country_3_code'];
                        
                if($postedData['1_country_title']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                       /* $languages[$key]['country_identity_code'] = $postedData['country_identity_code'];
                        $languages[$key]['country_name'] = $postedData['country_name'];
                       */ $languages[$key]['country_title'] = $postedData[$_lang['lang_id'].'_country_title'];
                        $languages[$key]['country_2_code'] = $postedData['country_2_code'];
                        $languages[$key]['country_3_code'] = $postedData['country_3_code'];
                        
                    }
                }    
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        /*if ($this->view->mode == 'Add') {
                            $countryData['country_createdate'] = date('Y-m-d H:i:s');
                        }
                        //country save
                        $countryData['country_id'] = $country_id;
                        $countryData['country_identity_code'] = $postedData['country_identity_code'];
                        $countryData['country_name'] = $postedData['country_name'];
                        $countryData['country_title'] = $postedData['country_title'];
                        $countryData['country_3_code'] = $postedData['country_3_code'];
                        $countryData['country_2_code'] = $postedData['country_2_code'];
                        $countryData['country_lang_id'] = $postedData['country_lang_id'];
                        $countryData['country_updatedate'] = date('Y-m-d H:i:s');
                        $countryData['country_status'] = $postedData['country_status'];
                        $countryModel->save($countryData, 'country_id');
                        */ if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_identity_code'] = strtolower($english_code);
                                $countryData['country_name'] = strtolower($english_code);
                                $countryData['country_title'] = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_2_code'] = $postedData['country_2_code'];
                                $countryData['country_3_code'] = $postedData['country_3_code'];
                                $countryData['country_createdate'] = date('Y-m-d H:i:s');
                                $countryData['country_updatedate'] = date('Y-m-d H:i:s');
                                $countryData['country_lang_id'] = $_lang['lang_id'];
                                $countryData['country_status'] = 1;
                                $countryData['country_ref_id'] = $_lang['country_ref_id'];
                                $last_id = $countryModel->save($countryData, 'country_id');
                                $countryData1['country_id'] = $last_id;
                                $countryData1['country_ref_id'] = $last_id;
                                $countryModel->save($countryData1, 'country_id');
                            }else{
                                $countryData['country_identity_code'] = strtolower($english_code);;
                                $countryData['country_name'] = strtolower($english_code);;
                                $countryData['country_title'] = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_2_code'] = $postedData['country_2_code'];
                                $countryData['country_3_code'] = $postedData['country_3_code'];
                                $countryData['country_createdate'] = date('Y-m-d H:i:s');
                                $countryData['country_updatedate'] = date('Y-m-d H:i:s');
                                $countryData['country_lang_id'] = $_lang['lang_id'];
                                $countryData['country_status'] = 1;
                                $countryData['country_ref_id'] = $last_id;
                                if($countryData['country_name']!= '' ){
                                    $countryModel->save($countryData, 'country_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_id'] = $_lang['country_id'];
                                $countryData['country_identity_code'] = strtolower($english_code);
                                $countryData['country_name'] = strtolower($english_code);
                                $countryData['country_title'] = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_2_code'] = $postedData['country_2_code'];
                                $countryData['country_3_code'] = $postedData['country_3_code'];
                                $countryData['country_lang_id'] = $_lang['lang_id'];
                                $countryData['country_status'] = 1;
                                $countryData['country_updatedate'] = date('Y-m-d H:i:s');
                                $countryData['country_ref_id'] = $country_ref_id;
                                $countryModel->save($countryData, 'country_id');
                                
                            }else{
                                $countryData['country_id'] = $_lang['country_id'];
                                $countryData['country_identity_code'] = strtolower($english_code);
                                $countryData['country_name'] = strtolower($english_code);
                                $countryData['country_title'] = $postedData[$_lang['lang_id'].'_country_title'];
                                $countryData['country_2_code'] = $postedData['country_2_code'];
                                $countryData['country_3_code'] = $postedData['country_3_code'];
                                $countryData['country_lang_id'] = $_lang['lang_id'];
                                $countryData['country_status'] = 1;
                                $countryData['country_updatedate'] = date('Y-m-d H:i:s');
                                $countryData['country_ref_id'] = $country_ref_id;
                                $countryModel->save($countryData, 'country_id');
                                
                            }
                        }
                     
                   }
                        
                        if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'Country add successfully';
                            } else {
                                $successmessage = 'Country updated successfully';
                            }
                            $this->addSuccessMessage($successmessage);
                            $this->_helper->redirector('list', 'country', 'default');
                        }
                    }
               
            }
            
            $this->view->languages = $languages;
            $this->view->countryid = $country_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
           // $this->view->form = $countryForm;


    }
    }?>